package net.technics;

import java.util.ArrayList;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Groups;
import net.models.Users;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.hibernate.Transaction;

public class GestionAdmin {
	private DataBindingContext	m_bindingContext;

	protected Shell				shlBackOfficeManagement;
	private Text				txtAjouterUnUtilisateur;
	private List<Users>			users	= new ArrayList<Users>();
	private List<Users>			admins	= new ArrayList<Users>();
	private List<Groups>		groups	= new ArrayList<Groups>();
	private ComboViewer			cmbViewerAdminARetirer;
	private Users				user	= new Users();
	private Table				table;
	private ComboViewer			cmbViewerAdminAAjouter;

	private CLabel				lblAdminRetire;

	private CLabel				lblAdminAjoute;
	private TableViewer			tableAdmin;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					GestionAdmin window = new GestionAdmin();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		users = Application.getUsers();
		groups = Application.getGroups();

		for (Users admin : users) {
			if (admin.getGroups().getId().equals(6)) {
				admins.add(admin);
			}
		}
		Display display = Display.getDefault();
		createContents();
		shlBackOfficeManagement.open();
		shlBackOfficeManagement.layout();
		while (!shlBackOfficeManagement.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlBackOfficeManagement = new Shell();
		shlBackOfficeManagement.setText("Back Office Management");
		shlBackOfficeManagement.setSize(458, 513);

		Composite composite = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		composite.setBounds(0, 0, 442, 48);

		txtAjouterUnUtilisateur = new Text(composite, SWT.READ_ONLY);
		txtAjouterUnUtilisateur.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtAjouterUnUtilisateur.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtAjouterUnUtilisateur.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		txtAjouterUnUtilisateur.setText("Gérer les droits d'accès");
		txtAjouterUnUtilisateur.setBounds(10, 10, 201, 21);

		Composite composite_2 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_2.setBounds(10, 54, 422, 48);

		CLabel lblUtilisateurModifier = new CLabel(composite_2, SWT.NONE);
		lblUtilisateurModifier.setBounds(10, 10, 234, 21);
		lblUtilisateurModifier.setText("Selectionner l'administrateur à retirer");

		cmbViewerAdminARetirer = new ComboViewer(composite_2, SWT.NONE);
		Combo combo = cmbViewerAdminARetirer.getCombo();
		combo.setBounds(250, 8, 162, 28);

		Composite composite_3 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_3.setBounds(214, 108, 218, 52);

		Button btnValider = new Button(composite_3, SWT.NONE);
		btnValider.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lblAdminRetire.setText("");
				IStructuredSelection sel = (IStructuredSelection) (cmbViewerAdminARetirer.getSelection());
				user = (Users) (sel.iterator().next());

				org.hibernate.Session session = (org.hibernate.Session) HibernateUtil.getSession();
				Transaction trans = session.beginTransaction();

				Groups responsable = new Groups();
				for (Groups group : groups) {
					if (group.getId().equals(4)) {
						responsable = group;
					}
				}

				// Ne marche pas
				// List<Users> admins = users;
				// for (Users user : users) {
				// if (!user.EstCeQueResponsable()) {
				// admins.remove(user);
				// }
				// }

				user.setGroups(responsable);
				session.update(user);
				trans.commit();
				session.close();
				lblAdminRetire.setText("L'administrateur" + user.getName() + "est maintenant un utilisateur");

				for (Users admin : users) {
					if (admin.getGroups().getId().equals(6)) {
						admins.add(admin);
					}
				}
			}
		});
		btnValider.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {

			}
		});
		btnValider.setText("Valider");
		btnValider.setBounds(22, 10, 90, 30);

		Button button_1 = new Button(composite_3, SWT.NONE);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shlBackOfficeManagement.close();
			}
		});
		button_1.setText("Annuler");
		button_1.setBounds(118, 10, 90, 30);

		Composite composite_1 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_1.setBounds(214, 255, 218, 52);

		Button button = new Button(composite_1, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lblAdminAjoute.setText("");
				IStructuredSelection sel = (IStructuredSelection) (cmbViewerAdminAAjouter.getSelection());
				user = (Users) (sel.iterator().next());

				org.hibernate.Session session = (org.hibernate.Session) HibernateUtil.getSession();
				Transaction trans = session.beginTransaction();

				Groups responsable = new Groups();
				for (Groups group : groups) {
					if (group.getId().equals(6)) {
						responsable = group;
					}
				}

				user.setGroups(responsable);
				session.update(user);
				trans.commit();
				session.close();
				lblAdminAjoute.setText("L'utilisateur" + user.getName() + "est maintenant un administrateur");

				for (Users admin : users) {
					if (admin.getGroups().getId().equals(6)) {
						admins.add(admin);
					}
				}
			}
		});
		button.setText("Valider");
		button.setBounds(22, 10, 90, 30);

		Button button_2 = new Button(composite_1, SWT.NONE);
		button_2.setText("Annuler");
		button_2.setBounds(118, 10, 90, 30);

		Composite composite_4 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_4.setBounds(10, 201, 422, 48);

		CLabel lblSelectionnerLadministrateur = new CLabel(composite_4, SWT.NONE);
		lblSelectionnerLadministrateur.setText("Selectionner l'administrateur à ajouter");
		lblSelectionnerLadministrateur.setBounds(10, 8, 234, 23);

		cmbViewerAdminAAjouter = new ComboViewer(composite_4, SWT.NONE);
		Combo combo_1 = cmbViewerAdminAAjouter.getCombo();
		combo_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

			}
		});
		combo_1.setBounds(250, 8, 162, 23);

		tableAdmin = new TableViewer(shlBackOfficeManagement, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableAdmin.getTable();
		table.setBounds(10, 350, 422, 114);

		lblAdminRetire = new CLabel(shlBackOfficeManagement, SWT.NONE);
		lblAdminRetire.setText("");
		lblAdminRetire.setBounds(10, 166, 234, 31);

		lblAdminAjoute = new CLabel(shlBackOfficeManagement, SWT.NONE);
		lblAdminAjoute.setText("");
		lblAdminAjoute.setBounds(10, 313, 234, 31);
		m_bindingContext = initDataBindings();
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), Users.class, new String[] { "name", "prenom", "id" });
		cmbViewerAdminARetirer.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		cmbViewerAdminARetirer.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Users.class).observe(users);
		cmbViewerAdminARetirer.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap[] observeMaps_1 = BeansObservables.observeMaps(listContentProvider_1.getKnownElements(), Users.class, new String[] { "name", "prenom" });
		cmbViewerAdminAAjouter.setLabelProvider(new ObservableMapLabelProvider(observeMaps_1));
		cmbViewerAdminAAjouter.setContentProvider(listContentProvider_1);
		//
		cmbViewerAdminAAjouter.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_2 = new ObservableListContentProvider();
		IObservableMap[] observeMaps_2 = BeansObservables.observeMaps(listContentProvider_2.getKnownElements(), Users.class, new String[] { "name", "prenom" });
		tableAdmin.setLabelProvider(new ObservableMapLabelProvider(observeMaps_2));
		tableAdmin.setContentProvider(listContentProvider_2);
		//
		IObservableList selfList_1 = Properties.selfList(Users.class).observe(admins);
		tableAdmin.setInput(selfList_1);
		//
		return bindingContext;
	}
}
